﻿using System;
using System.Collections.Generic;
using System.Text;
using DotNet.Model;
using DotNet.Common;
using DotNet.Data.Factory;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
namespace DotNet.Data
{
	public class AttributeDal
	{
		IDBHelp db = DBHelp.Create();
		Hashtable htSyn = Hashtable.Synchronized(new Hashtable());
		StringBuilder strSql = new StringBuilder();

		#region 是否存在该记录
		/// <summary>
		/// 是否存在该记录
		/// </summary>
		/// <param name="AttributeID">标识列ID</param>
		public bool Exists(int AttributeID)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("select count(1) from [Attribute] where AttributeID=@attributeid ");
			htSyn.Add("@attributeid", AttributeID);
			return db.IsExists(strSql.ToString(), htSyn);
		}
		#endregion

		#region 得到该表数据列表
		/// <summary>
		/// 得到该表数据列表
		/// </summary>
		public IList<Attribute> GetModel()
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
            strSql.Append("Select [AttributeID],[AttributeName],[Mark] FROM [Attribute] order by [Mark]");
			 return getAttributeBySql(strSql.ToString());
		}
		#endregion

		#region 根据标识列查询实体
		/// <summary>
		/// 根据标识列查询实体
		/// </summary>
		/// <param name="AttributeID">标识列ID</param>
		/// <returns></returns>
		public Attribute GetModel(int attributeid)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("select [AttributeName],[Mark] FROM [Attribute]");
			strSql.Append(" where AttributeID=@attributeid ");
			htSyn.Add("@AttributeID", attributeid);
			DataTable dt = db.GetDataTable(strSql.ToString(), htSyn);
			Attribute attributeModel = new Attribute();
			if (dt.Rows.Count > 0)
			{
				attributeModel.attributeName=dt.Rows[0]["AttributeName"].ToString();
                attributeModel.Mark =Convert.ToInt32(dt.Rows[0]["Mark"]);
			}
			return attributeModel;
		}
		#endregion

		#region 修改信息
		/// <summary>
		/// 修改信息
		/// </summary>
		/// <param name="Attribute">实体</param>
		/// <returns></returns>
		public bool Update(Attribute attributeModel)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("update [Attribute] set ");
			strSql.Append("[AttributeName]=@attributeName,");
            strSql.Append("[Mark]=@mark");
			strSql.Append(" where AttributeID=@attributeid ");
			htSyn.Add("@attributeid", attributeModel.attributeID);
			htSyn.Add("@attributename", attributeModel.attributeName);
            htSyn.Add("@mark",attributeModel.Mark);
			if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
			{
				return true;
			}
			return false;
		}
		#endregion

		#region 删除信息
		/// <summary>
		/// 删除信息
		/// </summary>
		/// <param name="AttributeID">标识列ID</param>
		/// <returns></returns>
		public bool Delete(int AttributeID)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("delete from [Attribute] where AttributeID=@attributeid");
			htSyn.Add("attributeid", AttributeID);
			if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
			{
				return true;
			}
			return false;
		}
		#endregion

		#region 添加信息
		/// <summary>
		/// 添加信息
		/// </summary>
		/// <param name="Attribute">实体</param>
		/// <returns></returns>
		public bool Insert(Attribute attributeModel)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("insert into [Attribute]([AttributeName],[Mark]) values (@attributename,@mark)");
			htSyn.Add("@attributename", attributeModel.attributeName);
            htSyn.Add("@mark",attributeModel.Mark);
			if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
			{
				return true;
			}
			return false;
		}
		#endregion

		#region 根据SQL语句获取集合
		/// <summary>
		///根据SQL语句获取集合
		/// </summary>
		/// <param name="strSql">sql语句</param>
		public IList<Attribute> getAttributeBySql(string strSql)
		{
			IList<Attribute> list = new List<Attribute>();
			DataTable dt = db.GetDataTable(strSql);
			if (dt.Rows.Count > 0)
			{
				foreach (DataRow dr in dt.Rows)
				{
					Attribute attributeModel = new Attribute();
					attributeModel.attributeID= Convert.ToInt32(dr["AttributeID"]);
					attributeModel.attributeName=dr["AttributeName"].ToString();
                    attributeModel.Mark = Convert.ToInt32(dr["Mark"]);
					list.Add(attributeModel);
				}
			}
			return list;
		}
		#endregion
	}
}
